package lz.student.student.controller;

import lz.student.student.dto.ScoreDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//修改了注释
@RequestMapping("teacher")
@Controller
public class TeacherController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("login")
    public String login() {
        return "/teacher/login";
    }

    @RequestMapping("index")
    public String index(HttpSession session,Model model) {
        if (!"admin".equals(session.getAttribute("account"))) {
            //当前没有登录admin
            return "/teacher/login";
        }
        String sql = "select id,id_card as idCard,name,score,evaluate from score ";
        List<ScoreDto> scoreDtos = jdbcTemplate.query(sql, new RowMapper<ScoreDto>() {
                    @Override
                    public ScoreDto mapRow(ResultSet rs, int rowNum) throws SQLException {
                        ScoreDto scoreDto = new ScoreDto();
                        scoreDto.setId(rs.getLong("id"));
                        scoreDto.setIdCard(rs.getString("idCard"));
                        scoreDto.setName(rs.getString("name"));
                        scoreDto.setScore(rs.getInt("score"));
                        scoreDto.setEvaluate(rs.getString("evaluate"));
                        return scoreDto;
                    }
                }
        );
        model.addAttribute("list", scoreDtos);
        return "/teacher/index";
    }


    @RequestMapping("doLogin")
    public String doLogin(String account, String password, Model model, HttpSession session) {

        if (account == null || account.isEmpty()) {
            model.addAttribute("errMsg", "账号不能为空");
            return "/teacher/login";
        }
        if (password == null || password.isEmpty()) {
            model.addAttribute("errMsg", "密码不能为空");
            return "/teacher/login";
        }
        if (!account.equals("admin") || !password.equals("admin")) {
            model.addAttribute("errMsg", "账号密码错误");
            return "/teacher/login";
        }
        session.setAttribute("account", account);


        return "redirect:/teacher/index";
    }


    @RequestMapping("delete")
    public String delete(Long id, HttpSession session) {
        if (!"admin".equals(session.getAttribute("account"))) {
            //当前没有登录admin
            return "/teacher/login";
        }
        String sql = "delete from score where id = ?";
        jdbcTemplate.update(sql, id);
        return "redirect:/teacher/index";
    }

    @RequestMapping("update")
    public String update(Long id, HttpSession session, Model model) {
        if (!"admin".equals(session.getAttribute("account"))) {
            //当前没有登录admin
            return "/teacher/login";
        }
        if (id != null) {
            String sql = "select id,id_card as idCard,name,score,evaluate from score where id = ? ";
            List<ScoreDto> scoreDtos = jdbcTemplate.query(sql, new RowMapper<ScoreDto>() {
                        @Override
                        public ScoreDto mapRow(ResultSet rs, int rowNum) throws SQLException {
                            ScoreDto scoreDto = new ScoreDto();
                            scoreDto.setId(rs.getLong("id"));
                            scoreDto.setIdCard(rs.getString("idCard"));
                            scoreDto.setName(rs.getString("name"));
                            scoreDto.setScore(rs.getInt("score"));
                            scoreDto.setEvaluate(rs.getString("evaluate"));
                            return scoreDto;
                        }
                    }, id
            );
            if (!scoreDtos.isEmpty()) {
                model.addAttribute("score", scoreDtos.get(0));
            } else {
                model.addAttribute("score", new ScoreDto());
            }
        }
        return "teacher/update";
    }


    @RequestMapping("save")
    public String save(HttpServletRequest request, HttpSession session, Model model) {
        if (!"admin".equals(session.getAttribute("account"))) {
            //当前没有登录admin
            return "/teacher/login";
        }

        String id = request.getParameter("id");
        if (id == null || id.isEmpty()) {
            //执行新增
            String sql = "INSERT INTO `score` (`id_card`, `name`, `score`, `evaluate`) VALUES (?, ?, ?, ?)";
            jdbcTemplate.update(sql, request.getParameter("idCard"), request.getParameter("name"),
                    request.getParameter("score"), request.getParameter("evaluate"));
        } else {
            //执行修改
            String sql = "update score set id_card = ? ,name = ? ,score = ?, evaluate = ? where id = ? ";
            jdbcTemplate.update(sql, request.getParameter("idCard"), request.getParameter("name"),
                    request.getParameter("score"), request.getParameter("evaluate"),id);
        }
        return "redirect:/teacher/index";
    }

    @RequestMapping("exit")
    public String exit(HttpSession session){
        session.invalidate();
        return "redirect:/teacher/login";
    }

}
